//
//  QudaobiangengViewController.m
//  BMProject
//
//  Created by chris on 16/1/2.
//  Copyright © 2016年 BMProject. All rights reserved.
//

#import "QudaobiangengViewController.h"
#import "DitchManageChangeCell.h"
#import "QudaobiangengMiddleCell.h"
#import "QudaobiangengBottomCell.h"
#import "DataModel.h"
#import "ReasonViewController.h"
#import "UIViewController+common.h"
#import "DitchMsgChangeVpnCell.h"
#import "DetailBottomCell.h"
#import "DitchManageChangeVc.h"
#import "DitchMsgChangeAddressCell.h"
#import "DitchMsgChangeNameCell.h"
#import "DitchMsgChangeBankCell.h"

@interface QudaobiangengViewController () {
    OrderDealDetailOrderInfo *_orderInfo;
    OrderDealDetailOrderContentInfo *_contentInfo;
    UITableView *_tableview;
    NSMutableArray *_orderDealDetailPoolList;//OrderDealDetailPoolList 数据
}
@end

@interface QudaobiangengViewController () <UITableViewDataSource, UITableViewDelegate>{
    NSString *_imgStr;
    NSString *_imgStr2;

}

@end

@implementation QudaobiangengViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NAV_INIT(self, @"门店信息变更", @"back", @selector(backButtonClick:), nil, nil);
    [self requestDetail];

}

- (void)initViewAfterRequest {
    _ditchType = _contentInfo.secondType.intValue;
    UITableView *tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    tableview.delegate = self;
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableview.sectionHeaderHeight = 8.0;
    tableview.sectionFooterHeight = 8.0;
    tableview.dataSource = self;
    tableview.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEF_SCREEN_WIDTH, 10)];
    _tableview = tableview;
    [self addSubview:tableview];
}

#pragma mark-UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if(_type == ORDER_LOOK_TYPE){
        switch (_ditchType) {
            case DITCH_TYPE_NAME_CHANGE:
                return 4-1;
            case DITCH_TYPE_ADDRESS_CHANGE:
                return 3-1;
            case DITCH_TYPE_BANK_CHANGE:
                return 5-1;
            case DITCH_TYPE_VPN_CHANGE:
                return 3-1;
        }
    }else{
        switch (_ditchType) {
            case DITCH_TYPE_NAME_CHANGE:
                return 4;
            case DITCH_TYPE_ADDRESS_CHANGE:
                return 3;
            case DITCH_TYPE_BANK_CHANGE:
                return 5;
            case DITCH_TYPE_VPN_CHANGE:
                return 3;
        }
    }

    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (_ditchType == DITCH_TYPE_ADDRESS_CHANGE) {
            static NSString *cellId = @"DitchMsgChangeAddressCell";
            DitchMsgChangeAddressCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_model];
            [cell setOrderContentInfo:_contentInfo];
            return cell;
        }else if (_ditchType == DITCH_TYPE_NAME_CHANGE) {
            static NSString *cellId = @"DitchMsgChangeNameCell";
            DitchMsgChangeNameCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_model];
            [cell setOrderContentInfo:_contentInfo];
            return cell;
        }else if (_ditchType == DITCH_TYPE_BANK_CHANGE) {
            static NSString *cellId = @"DitchMsgChangeBankCell";
            DitchMsgChangeBankCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_model];
            [cell setOrderContentInfo:_contentInfo];
            return cell;
        } else {
            static NSString *cellId = @"DitchMsgChangeVpnCell";
            DitchMsgChangeVpnCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_model];
            [cell setOrderContentInfo:_contentInfo];
            return cell;
        }
    }
    else if (indexPath.section == 1) {
        if (_ditchType == DITCH_TYPE_NAME_CHANGE) {
            static NSString *cellId = @"QudaobiangengMiddleCell";
            QudaobiangengMiddleCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_imgStr AndType:@"门店正面照"];
            return cell;
        } else if (_ditchType == DITCH_TYPE_BANK_CHANGE) {
            static NSString *cellId = @"QudaobiangengMiddleCell";
            QudaobiangengMiddleCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_imgStr2 AndType:@"身份证扫描件"];
            return cell;
        } else {
            static NSString *cellId = @"QudaobiangengBottomCell";
            QudaobiangengBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_contentInfo];
            return cell;
        }
    }
    else if (indexPath.section == 2) {
        if (_ditchType == DITCH_TYPE_VPN_CHANGE || _ditchType == DITCH_TYPE_ADDRESS_CHANGE) {
            static NSString *cellId = @"DetailBottomCell";
            DetailBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                [cell.passBtn addTarget:self action:@selector(passClick) forControlEvents:UIControlEventTouchUpInside];
                [cell.rejectBtn addTarget:self action:@selector(rejectClick) forControlEvents:UIControlEventTouchUpInside];
            }
            return cell;
        } else if (_ditchType == DITCH_TYPE_BANK_CHANGE) {
            static NSString *cellId = @"QudaobiangengMiddleCell";
            QudaobiangengMiddleCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_imgStr AndType:@"银行账号变更单据扫描件"];
            return cell;
        } else {
            static NSString *cellId = @"QudaobiangengBottomCell";
            QudaobiangengBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_contentInfo];
            return cell;
        }
    }else if (indexPath.section == 3) {
        if (_ditchType == DITCH_TYPE_BANK_CHANGE) {
            static NSString *cellId = @"QudaobiangengBottomCell";
            QudaobiangengBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
            }
            [cell setCellInfo:_contentInfo];
            return cell;
        }else{
            static NSString *cellId = @"DetailBottomCell";
            DetailBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
            if (!cell) {
                cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                [cell.passBtn addTarget:self action:@selector(passClick) forControlEvents:UIControlEventTouchUpInside];
                [cell.rejectBtn addTarget:self action:@selector(rejectClick) forControlEvents:UIControlEventTouchUpInside];
            }
            return cell;
        }

    } else {
        static NSString *cellId = @"DetailBottomCell";
        DetailBottomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:cellId owner:self options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell.passBtn addTarget:self action:@selector(passClick) forControlEvents:UIControlEventTouchUpInside];
            [cell.rejectBtn addTarget:self action:@selector(rejectClick) forControlEvents:UIControlEventTouchUpInside];
        }
        return cell;
    }
}

#pragma mark-UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    switch (_ditchType) {
        case DITCH_TYPE_NAME_CHANGE:
            if (indexPath.section == 0) {
                return 180;
            }
            if (indexPath.section == 1) {
                return 250;
            }
            if (indexPath.section == 1) {
                return 80;
            }
            else {
                return 60;
            }
        case DITCH_TYPE_ADDRESS_CHANGE:
            if (indexPath.section == 0) {
                return 240;
            }
            if (indexPath.section == 1) {
                return 80;
            }
            else {
                return 60;
            }
        case DITCH_TYPE_BANK_CHANGE:
            if (indexPath.section == 0) {
                return 350;
            }
            if (indexPath.section == 1) {
                return 250;
            }
            if (indexPath.section == 2) {
                return 250;
            }
            else {
                return 60;
            }
        case DITCH_TYPE_VPN_CHANGE:
            if (indexPath.section == 0) {
                return 180;
            }
            if (indexPath.section == 1) {
                return 80;
            }
            else {
                return 60;
            }
        default:
            return 180;
    }

}

- (void)requestDetail {
    //Type=2 类型为号卡工单时的返回报文
    [UserServices orderAuditDetailByToen:[[AppDelegate appDelegate] getUserInfo].token
                                      id:_model.id
                              showDialog:YES
                         completionBlock:^(int result, id responseObject) {
                             if (result == API_Request_Success) {
                                 NSDictionary *responseDict = responseObject[0];
                                 NSDictionary *orderInfoDic = [responseDict objectForKey:@"orderInfo"];
                                 NSDictionary *orderContentInfoDic = [responseDict objectForKey:@"orderContentInfo"];
                                 NSMutableArray *poolList = [responseDict objectForKey:@"poolList"];//数组
                                 _orderInfo = [[OrderDealDetailOrderInfo alloc] initWithDictionary:orderInfoDic error:nil];
                                 _contentInfo = [[OrderDealDetailOrderContentInfo alloc] initWithDictionary:orderContentInfoDic error:nil];
                                 [self initViewAfterRequest];
                                 if(_ditchType == DITCH_TYPE_NAME_CHANGE || _ditchType == DITCH_TYPE_BANK_CHANGE){
                                     NSDictionary *imgMap = [responseDict objectForKey:@"imgMap"];
                                     NSDictionary *key = imgMap[@"CH_LEASE_KEY"];
                                     NSDictionary *keyTwo = imgMap[@"CH_PERSONAGE_KEY_TWO"];
                                     NSString *fileWebPath = key[@"fileWebPath"];
                                     NSString *twoPath = keyTwo[@"fileWebPath"];
                                     _imgStr = [BASE_IMG_URL stringByAppendingString: [NSString ignoreEmptyStr:fileWebPath]];
                                     _imgStr2 = [BASE_IMG_URL stringByAppendingString: [NSString ignoreEmptyStr:twoPath]];
                                 }
                                 // 门面照  变更信息 未找到
                             }
                         }];
//    [UserServices orderAuditDetailByToen:[[AppDelegate appDelegate] getUserInfo].token
//                                      id:_model.id
//                                    sign:_model.sign
//                                    type:_model.type
//                              showDialog:YES
//                         completionBlock:^(int result, id responseObject) {
//                             if (result == API_Request_Success) {
//                                 NSDictionary *responseDict = responseObject[0];
//                                 NSDictionary *orderInfoDic = [responseDict objectForKey:@"orderInfo"];
//                                 NSDictionary *orderContentInfoDic = [responseDict objectForKey:@"orderContentInfo"];
//                                 NSMutableArray *poolList = [responseDict objectForKey:@"poolList"];//数组
//                                 _orderInfo = [[OrderDealDetailOrderInfo alloc] initWithDictionary:orderInfoDic error:nil];
//                                 _contentInfo = [[OrderDealDetailOrderContentInfo alloc] initWithDictionary:orderContentInfoDic error:nil];
//                                 [self initViewAfterRequest];
//                                 // 门面照  变更信息 未找到
//                             }
//                         }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark-event pass
- (void)passClick
{
    DLog(@"审核通过..");
    [UserServices orderAuditOpttByToken:[[AppDelegate appDelegate] getUserInfo].token
                                     id:_model.id
                                basicId:_model.id
                                 status:@"pass"
                                  reson:@""
                             showDialog:YES
                        completionBlock:^(int result, id responseObject) {
                            [self backButtonClick:nil];
                        }];

}

#pragma mark-event rejet
- (void)rejectClick
{
    DLog(@"审核驳回..");
    ReasonViewController * vc = [[ReasonViewController alloc]init];
    vc.isFromeOrder = YES;
    vc.orderID = _model.id;
    [self.navigationController pushViewController:vc animated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
